from django.shortcuts import render

# Create your views here.
import re
from django.core.mail import send_mail  # 邮箱需要
from django.conf import settings

from rest_framework.views import APIView
from rest_framework.response import Response
from .serializer import *


class GradeView(APIView):  # 等级表的增删改查
    def post(self, request):
        print(request.data)
        if GradeModel.objects.filter(grade=request.data.get('grade')):
            return Response({'msg': '该等级已存在', 'code': 400})
        ser = GradeSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '添加成功', 'code': 200})
        else:
            print(ser.errors)
            return Response({'msg': '添加失败', 'code': 400})

    def get(self, request):
        grade_obj = GradeModel.objects.all()
        ser = GradeSer(grade_obj, many=True)
        return Response(ser.data)

    def put(self, request, grade_id):
        grade_obj = GradeModel.objects.filter(pk=grade_id).first()
        if grade_obj:
            grade_obj.grade = request.data.get('grade')
            grade_obj.save()
            return Response({'msg': '修改成功', 'code': 200})
        else:
            return Response({'msg': '该等级不存在', 'code': 400})

    def delete(self, request, grade_id):
        grade_obj = GradeModel.objects.filter(pk=grade_id)
        if grade_obj:
            grade_obj.delete()
            return Response({'msg': '删除成功', 'code': 200})
        else:
            return Response({'msg': '该分类不存在', 'code': 400})


class UserView(APIView):  # 用户表的增删改查
    def post(self, request):
        print(request.data)
        name = request.data.get('name')
        password1 = request.data.get('password1')
        password2 = request.data.get('password2')
        state = request.data.get('state')
        grade = request.data.get('grade')
        email = request.data.get('email')
        grade_obj = GradeModel.objects.filter(pk=grade).first()
        print(grade_obj)
        if not re.match(r'[0-9a-zA_]{0,19}@(.*?).com', email):  # 判断邮箱
            return Response({'msg': '邮箱不符合规则', 'code': 400})
        user_obj = UserModel.objects.filter(name=name).first()
        if user_obj:
            return Response({'msg': '用户已存在', 'code': 400})
        if password1 == password2:
            data = {
                'name': name,
                'password': password2,
                'state': state,
                'grade': grade,
                'email': email
            }
            ser = UserSer(data=data)
            if ser.is_valid():
                ser.save()
                subject = '好友多会员注册'  # 主题
                message = '欢迎注册好又多'  # 消息
                from_email = settings.EMAIL_FROM  # 需要导入settings
                recipient_list = [email]
                send_mail(subject=subject,
                          message=message,
                          from_email=from_email,
                          recipient_list=recipient_list)
                return Response({'msg': '注册成功', 'code': 200})
            else:
                print(ser.errors)
                return Response({'msg': '注册失败', 'code': 400})
        else:
            return Response({'msg': '两次密码不一致', 'code': 400})

    def get(self, request):
        user_obj = UserModel.objects.all()
        ser = UserSer(user_obj, many=True)
        return Response(ser.data)

    def put(self, request, uid):
        user_obj = UserModel.objects.filter(pk=uid).first()
        ser = UserSer(user_obj, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '修改成功', 'code': 200})
        else:
            return Response({'msg': '修改失败', 'code': 400})

    def delete(self, request, uid):
        user_obj = UserModel.objects.filter(pk=uid)
        if user_obj:
            user_obj.delete()
            return Response({'msg': '删除成功', 'code': 400})
        else:
            return Response({'msg': '删除失败', 'code': 400})


class CateView(APIView):  # 分类表的增删改查
    def post(self, request):
        print(request.data)
        if CateModel.objects.filter(name=request.data.get('name')):
            return Response({'msg': '该分类已存在', 'code': 400})
        ser = CateSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '添加成功', 'code': 200})
        else:
            print(ser.errors)
            return Response({'msg': '添加失败', 'code': 400})

    def get(self, request):
        cate_obj = CateModel.objects.all()
        ser = CateSer(cate_obj, many=True)
        return Response(ser.data)

    def put(self, request, cid):
        cate_obj = CateModel.objects.filter(pk=cid).first()
        ser = CateSer(cate_obj, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '修改成功', 'code': 200})
        else:
            return Response({'msg': '修改失败', 'code': 400})

    def delete(self, request, cid):
        cate_obj = CateModel.objects.filter(pk=cid)
        if cate_obj:
            cate_obj.delete()
            return Response({'msg': '删除成功', 'code': 400})
        else:
            return Response({'msg': '删除失败', 'code': 400})


class FictionView(APIView):  # 小说表的增删改查
    def post(self, request):
        print(request.data)
        if FictionModel.objects.filter(name=request.data.get('name')):
            return Response({'msg': '该小说已存在', 'code': 400})
        ser = FictionSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '添加成功', 'code': 200})
        else:
            print(ser.errors)
            return Response({'msg': '添加失败', 'code': 400})

    def get(self, request):
        fiction_obj = FictionModel.objects.all()
        ser = GradeSer(fiction_obj, many=True)
        return Response(ser.data)

    def put(self, request, fid):
        fiction_obj = FictionModel.objects.filter(pk=fid).first()
        ser = UserSer(fiction_obj, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '修改成功', 'code': 200})
        else:
            return Response({'msg': '修改失败', 'code': 400})

    def delete(self, request, fid):
        fiction_obj = UserModel.objects.filter(pk=fid)
        if fiction_obj:
            fiction_obj.delete()
            return Response({'msg': '删除成功', 'code': 400})
        else:
            return Response({'msg': '删除失败', 'code': 400})


class SectionView(APIView):  # 章节的增删改查
    def post(self, request):
        print(request.data)
        if SectionModel.objects.filter(name=request.data.get('name')):
            return Response({'msg': '该章节已存在', 'code': 400})
        ser = SectionSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '添加成功', 'code': 200})
        else:
            print(ser.errors)
            return Response({'msg': '添加失败', 'code': 400})

    def get(self, request):
        section_obj = SectionModel.objects.all()
        ser = SectionSer(section_obj, many=True)
        return Response(ser.data)

    def put(self, request, sid):
        section_obj = SectionModel.objects.filter(pk=sid).first()
        ser = SectionSer(section_obj, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '修改成功', 'code': 200})
        else:
            return Response({'msg': '修改失败', 'code': 400})

    def delete(self, request, fid):
        section_obj = SectionModel.objects.filter(pk=fid)
        if section_obj:
            section_obj.delete()
            return Response({'msg': '删除成功', 'code': 400})
        else:
            return Response({'msg': '删除失败', 'code': 400})
